home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_13_11
/
phillip2
/
segment2.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-14
|
27KB
|
932 lines
/***********************************************
*
* file d:\cips\segment2.c
*
* Functions: This file contains
* find_cutoff_point
* edge_region
* gray_shade_region
* edge_gray_shade_region
* pixel_grow
* pixel_label_and_check_neighbors
* is_close
* erode_image_array
* get_edge_region_options
*
* Purpose:
* These function implement the three
* segmentation techniques in Image
* Processing part 10.
*
* External Calls:
* wtiff.c - round_off_image_size
* create_file_if_needed
* write_array_into_tiff_image
* tiff.c - read_tiff_header
* rtiff.c - read_tiff_image
* numcvrt.c - get_integer
* edges.c - quick_edge
* homogeneity
* difference_edge
* contrast_edge
* gaussian_edge
* range
* variance
* detect_edges
* hist.c - calculate_histogram
* zero_histogram
* thresh.c - threshold_image_array
*
* Modifications:
* 5 December 1992 - created
*
*************************************************/
#include "cips.h"
/*******************************************
*
* find_cutoff_point(..
*
* This function looks at a histogram
* and sets a cuttoff point at a given
* percentage of pixels.
* For example, if percent=0.6, you
* start at 0 in the histogram and count
* up until you've hit 60% of the pixels.
* Then you stop and return that pixel
* value.
*
********************************************/
find_cutoff_point(histogram, percent, cutoff)
unsigned long histogram[];
float percent;
short *cutoff;
{
float fd, fsum, sum_div;
int i, looking;
long lc, lr, num=0, sum=0;
sum = 0;
i = 0;
lr = (long)(ROWS);
lc = (long)(COLS);
num = lr*lc;
fd = (float)(num);
while(looking){
fsum = (float)(sum);
sum_div = fsum/fd;
if(sum_div >= percent)
looking = 0;
else
sum = sum + histogram[i++];
} /* ends while looking */
if(i >= 256) i = 255;
*cutoff = i;
printf("\nCutoff is %d sum=%ld", *cutoff, sum);
} /* ends find_cutoff_point */
/*******************************************
*
* edge_region(..
*
* This function segments an image by
* growing regions inside of edges.
* The steps are:
* . detect edges
* . threshold edge output to a
* percent value
* . remove edges from consideration
* . grow regions
*
*******************************************/
edge_region(in_name, out_name, the_image, out_image,
il, ie, ll, le, edge_type, min_area,
max_area, diff, percent, set_value,
erode)
char in_name[], out_name[];
float percent;
int edge_type, il, ie, ll, le;
short diff, erode,
max_area, min_area,
set_value,
the_image[ROWS][COLS],
out_image[ROWS][COLS];
{
int a, b, count, i, j, k,
length, width;
short cutoff;
struct tiff_header_struct image_header;
unsigned long histogram[GRAY_LEVELS+1];
create_file_if_needed(in_name, out_name, out_image);
read_tiff_image(in_name, the_image, il, ie, ll, le);
/***************************
*
* Detect the edges. Do
* not threshold.
*
****************************/
if(edge_type == 1 ||
edge_type == 2 ||
edge_type == 3)
detect_edges(in_name, out_name, the_image,
out_image, il, ie, ll, le,
edge_type, 0, 0);
if(edge_type == 4){
quick_edge(in_name, out_name, the_image,
out_image, il, ie, ll, le,
0, 0);
} /* ends if 4 */
if(edge_type == 5){
homogeneity(in_name, out_name, the_image,
out_image, il, ie, ll, le,
0, 0);
} /* ends if 5 */
if(edge_type == 6){
difference_edge(in_name, out_name, the_image,
out_image, il, ie, ll, le,
0, 0);
} /* ends if 6 */
if(edge_type == 7){
contrast_edge(in_name, out_name, the_image,
out_image, il, ie, ll, le,
0, 0);
} /* ends if 7 */
if(edge_type == 8){
gaussian_edge(in_name, out_name, the_image,
out_image, il, ie, ll, le,
3, 0, 0);
} /* ends if 8 */
if(edge_type == 10){
range(in_name, out_name, the_image,
out_image, il, ie, ll, le,
3, 0, 0);
} /* ends if 10 */
if(edge_type == 11){
variance(in_name, out_name, the_image,
out_image, il, ie, ll, le,
0, 0);
} /* ends if 11 */
/**write_array_into_tiff_image("f:e1.tif", out_image,
il, ie, ll, le);**/
/* copy out_image to the_image */
for(i=0; i<ROWS; i++)
for(j=0; j<COLS; j++)
the_image[i][j] = out_image[i][j];
/******************************
*
* Threshold the edge detector
* output at a given percent.
* This eliminates the weak
* edges.
*
*******************************/
zero_histogram(histogram);
calculate_histogram(the_image, histogram);
find_cutoff_point(histogram, percent, &cutoff);
threshold_image_array(the_image, out_image,
255, cutoff, set_value);
/**write_array_into_tiff_image("f:e2.tif", out_image,
il, ie, ll, le);**/
if(erode != 0){
/* copy out_image to the_image */
for(i=0; i<ROWS; i++)
for(j=0; j<COLS; j++)
the_image[i][j] = out_image[i][j];
erode_image_array(the_image, out_image,
set_value, erode);
} /* ends if erode */
/**write_array_into_tiff_image("f:e3.tif", out_image,
il, ie, ll, le);**/
/*******************************
*
* Set all the edge values to
* FORGET_IT so the region
* growing will not use those
* points.
*
*******************************/
for(i=0; i<ROWS; i++)
for(j=0; j<COLS; j++)
if(out_image[i][j] == set_value)
out_image[i][j] = FORGET_IT;
for(i=0; i<ROWS; i++)
for(j=0; j<COLS; j++)
the_image[i][j] = out_image[i][j];
pixel_grow(the_image, out_image, diff,
min_area, max_area);
write_array_into_tiff_image(out_name, out_image,
il, ie, ll, le);
} /* ends edge_region */
/*******************************************
*
* gray_shade_region(...
*
* This function segments an image by
* growing regions based only on gray
* shade.
*
*******************************************/
gray_shade_region(in_name, out_name, the_image,
out_image, il, ie, ll, le,
diff, min_area, max_area)
char in_name[], out_name[];
int il, ie, ll, le;
short the_image[ROWS][COLS],
out_image[ROWS][COLS],
diff, min_area, max_area;
{
int a, b, big_count, count, i, j, k, l,
not_finished, length, width;
short temp[3][3];
struct tiff_header_struct image_header;
create_file_if_needed(in_name, out_name, out_image);
read_tiff_image(in